package com.qf.team2.netdisc.admin.controller;

import com.qf.team2.netdisc.admin.service.AdminLoginService;
import com.qf.team2.netdisc.common.base.BaseController;
import com.qf.team2.netdisc.common.pojo.entity.User;
import com.qf.team2.netdisc.common.pojo.qo.UserQO;
import com.qf.team2.netdisc.common.pojo.vo.RespResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * 管理员登录接口
 *
 * @author Simon_Wong
 * @since 2021/5/26 14:42
 */
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
@Controller
@RequestMapping("/admin")
@Api(tags = "管理员登录接口")
public class AdminLoginController extends BaseController<Object> {

    @Autowired
    private AdminLoginService adminLoginService;

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation("管理员登录")
    public RespResultVO<Object> login(@RequestBody UserQO userQO,
                                      HttpServletResponse resp) {
        User dbUser = adminLoginService.login(userQO.getUsername(), userQO.getPassword());

        Cookie cookie = new Cookie("userId", dbUser.getId().toString());
        cookie.setMaxAge(60 * 60 * 24 * 7);
        cookie.setPath("/");
        resp.addCookie(cookie);

        Cookie cookie1 = new Cookie("username", dbUser.getUsername());
        cookie1.setMaxAge(60 * 60 * 24 * 7);
        cookie1.setPath("/");
        resp.addCookie(cookie1);

        Cookie cookie2 = new Cookie("character", dbUser.getCharacter().toString());
        cookie2.setMaxAge(60 * 60 * 24 * 7);
        cookie2.setPath("/");
        resp.addCookie(cookie2);

        /*resp.addCookie(new Cookie("userId", dbUser.getId().toString()));
        resp.addCookie(new Cookie("username", dbUser.getUsername()));
        resp.addCookie(new Cookie("character", dbUser.getCharacter().toString()));*/
        return successResp(dbUser, "登录成功");
    }

}
